﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Mvc;
using Ext.Net;
using Ext.Net.MVC;
using Sharp.DataTransferObject;
using Sharp.Infrastructure;
using Sharp.Infrastructure.WebApiClient;
using Sharp.Portal.ExtNet;
using Sharp.Portal.Mvc;
using Sharp.ServiceContracts;
using Sharp.WebBooter.Areas.Platform.Models;
using PartialViewResult = Ext.Net.MVC.PartialViewResult;
// ReSharper disable Mvc.ViewNotResolved
#pragma warning disable 618

#endregion

namespace Sharp.WebBooter.Areas.Platform.Controllers
{
    [Authorize]
    [AutomaticTemplate]
    public class MasterDataController : Controller
    {
        public ActionResult Index(string containerId)
        {
            var model = new MasterDataIndexModel();


            return new PartialViewResult
            {
                ViewName = "Index",
                Model = model,
                ContainerId = containerId,
                RenderMode = RenderMode.AddTo
            };
        }

        public ActionResult LeftTree()
        {
            return View(new MasterDataCategoryTreeViewModel()
            {
                StoreUrlAction = "GetCategoryData"
            });
        }


        /// <summary>
        /// 分类数据
        /// </summary>
        /// <returns></returns>
        public ActionResult GetCategoryData()
        {
            using (var proxy = new WebApiClientServiceProxy<IPlatformService>())
            {
                var data = proxy.Channel.GetMasterDataCategory();

                return this.Store(data);
            }
        }
        public ActionResult Create(string category)
        {
            if (category.IsNullOrEmpty())
            {
                var res = new Result();
                res.Err = "分类不能为空";

                return this.JsonNet(res);
            }
            return new PartialViewResult { Model = new MasterDataModel() { Category = category } };
        }

        public ActionResult Edit(Guid id)
        {
            MasterDataModel vm;
            using (var proxy = new WebApiClientServiceProxy<IPlatformService>())
            {
                vm = proxy.Channel.GetMasterDataInfo(id).To<MasterDataModel>();
                vm.DropDown = proxy.Channel.GetMasterData();
            }

            return new PartialViewResult { Model = vm };
        }

        public ActionResult Details(Guid id)
        {
            MasterDataModel vm;
            using (var proxy = new WebApiClientServiceProxy<IPlatformService>())
            {
                vm = proxy.Channel.GetMasterDataInfo(id).To<MasterDataModel>();
                vm.DropDown = proxy.Channel.GetMasterData();
            }

            return new PartialViewResult { Model = vm };
        }
        [DirectMethod]
        public ActionResult Save(MasterDataModel model)
        {
            Result res;
            using (var proxy = new WebApiClientServiceProxy<IPlatformService>())
            {
                res = proxy.Channel.SaveMasterData(model.To<MasterDataDTO>());
            }

            var dr = this.Direct();
            if (res.Err.Length == 0)
            {
                dr.Script = string.Format("Ext.getCmp('{0}').getStore().reload();", model.UI.RefreshGrid);
            }
            return dr;
        }

        #region 分页数据
        /// <summary>
        /// 分页数据
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ActionResult GetPaged(ExtRequestParameters parameters)
        {
            using (var proxy = new WebApiClientServiceProxy<IPlatformService>())
            {
                var pageInfo = proxy.Channel.MasterDataGetPaged(parameters.ToPagingFeature());

                return this.Store(pageInfo.Data, pageInfo.TotalRecords);
            }


        }
        #endregion

        [System.Web.Mvc.HttpPost]
        public ActionResult Delete(Guid[] ids)
        {
            using (var proxy = new WebApiClientServiceProxy<IPlatformService>())
            {
                var res = proxy.Channel.DeleteMasterData(ids);

                return this.JsonNet(res);
            }
        }

        public ActionResult GetMasterDataSort()
        {
            IList<MasterDataCategoryDTO> list;
            using (var proxy = new WebApiClientServiceProxy<IPlatformService>())
            {
                list = proxy.Channel.GetMasterDataSort();
            }
            return this.JsonNet(list);
        }
        public ActionResult GetMasterData()
        {
            IList<MasterDataSimpleDTO> list;
            using (var proxy = new WebApiClientServiceProxy<IPlatformService>())
            {
                list = proxy.Channel.GetMasterData();
            }
            return this.JsonNet(list);
        }
        public ActionResult MasterGrid()
        {
            var model = new MasterDataIndexModel()
            {
            };
            return View(model);
        }
        #region 分类
        public ActionResult CategoryCreate()
        {
            var vm = new MasterDataCategoryViewModel();
            vm.UI.PostAction = "CategorySave";
            return new PartialViewResult
            {
                Model = vm
            };
        }

        public ActionResult CategoryEdit(int id)
        {
            MasterDataCategoryViewModel vm;
            using (var proxy = new WebApiClientServiceProxy<IPlatformService>())
            {
                vm = proxy.Channel.GetMasterDataCategoryInfo(id).To<MasterDataCategoryViewModel>();
            }
            vm.UI.PostAction = "CategorySave";
            return new PartialViewResult { Model = vm };
        }
        [System.Web.Mvc.HttpPost]
        public ActionResult CategoryDelete(int[] ids)
        {
            using (var proxy = new WebApiClientServiceProxy<IPlatformService>())
            {
                var res = proxy.Channel.DeleteMasterDataCategory(ids);

                return this.JsonNet(res);
            }
        }
        [System.Web.Mvc.HttpPost]
        public ActionResult CategorySave(MasterDataCategoryViewModel model)
        {
            using (var proxy = new WebApiClientServiceProxy<IPlatformService>())
            {
                var res = proxy.Channel.SaveMasterDataCategory(model.To<MasterDataCategoryDTO>());

                return this.JsonNet(res);
            }
        }

        #endregion
    }
}